package com.xdjd.mobile.system;

import com.alibaba.fastjson.JSON;
import com.gitee.easyopen.ApiContext;
import com.xdjd.mobile.business.entity.vo.LoginUser;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpSession;

public class ApiUtil {

    private static String USER_KEY = "xdjd-user";

    /**
     * 获取当前登录用户
     */
    public static LoginUser getCurrentUser() {
        HttpSession session = ApiContext.getManagedSession();
        if (session == null) {
            return null;
        }
        String userJson = (String)session.getAttribute(USER_KEY);
        return StringUtils.isEmpty(userJson) ? null : JSON.parseObject(userJson, LoginUser.class);
    }

    /**
     * 保存登录用户到session
     *
     * @param user
     */
    public static void setCurrentUser(LoginUser user) {
        HttpSession session = ApiContext.getManagedSession();
        if (session != null) {
            session.setAttribute(USER_KEY, JSON.toJSONString(user));
        }
    }

}
